//
//  WebsiteItem.m
//  BaoVn
//
//  Created by Phuong Le on 11/8/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "WebsiteItemView.h"
#import "BadgeButton.h"
#import "WebsiteCollectionViewDelegate.h"

@implementation WebsiteItemView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (void)initialize
{
    // Dummy data
    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"vnexpress.png", ICON_KEY,
                      @"mazda6.jpg", @"image",
                      @"1", UNREAD_NEWS_COUNT_KEY,
                      @"Chiếc Mazda có thiết kế đột phá nhất", TITLE_KEY,
                      @"Lần đầu tiên 1 chiếc sedan hạng trung lại được xếp vào hàng có thiết kế đẹp với siêu xe", DESCRIPTION_KEY,
                      @"1 giờ trước", CREATED_DATE_KEY, nil];
    
    // News image
    _newsHeadImageView = [[UIImageView alloc] init];
    _newsHeadImageView.contentMode = UIViewContentModeScaleAspectFill;
    [_newsHeadImageView setClipsToBounds:YES];
    [self addSubview:_newsHeadImageView];
    
    // Website icon
    _websiteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _websiteButton.backgroundColor = COLOR(0, 0, 0, .3f);
    [_websiteButton addTarget:self action:@selector(websiteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    _websiteButton.contentMode = UIViewContentModeScaleAspectFit;
    _websiteButton.imageEdgeInsets = UIEdgeInsetsMake(8, 8, 8, 8);
    
    [self addSubview:_websiteButton];
    
    // Website badge
    _unreadItemsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_unreadItemsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _unreadItemsButton.backgroundColor = COLOR_RED_NORMAL;
    [self addSubview:_unreadItemsButton];
    
    // Short news
    _latestNewsDiscriptionLabel = [[UILabel alloc] init];
    _latestNewsDiscriptionLabel.numberOfLines = 0;
    [_latestNewsDiscriptionLabel sizeToFit];
    _latestNewsDiscriptionLabel.backgroundColor = [UIColor clearColor];
    _latestNewsDiscriptionLabel.textColor = [UIColor whiteColor];
    _latestNewsDiscriptionLabel.font = FONT(13);
    [self addSubview:_latestNewsDiscriptionLabel];
    
    _latestNewsDateLabel = [[UILabel alloc] init];
    _latestNewsDateLabel.numberOfLines = 1;
    _latestNewsDateLabel.backgroundColor = [UIColor clearColor];
    _latestNewsDateLabel.textColor = [UIColor whiteColor];
    _latestNewsDateLabel.font = FONT(11);
    [self addSubview:_latestNewsDateLabel];
    
    // Gradient discription layer
    _gradientDiscriptionLayer = [CAGradientLayer layer];
    
    _gradientDiscriptionLayer.colors = @[(id)[[UIColor colorWithWhite:1.0f alpha:0.0f] CGColor],
                                         (id)[[UIColor colorWithWhite:0.3f alpha:0.7f] CGColor],
                                         (id)[[UIColor colorWithWhite:0.0f alpha:1.0f] CGColor]];
    _gradientDiscriptionLayer.locations = @[@0.00f, @0.50f, @1.00f];
    
    [self.layer insertSublayer:_gradientDiscriptionLayer below:_latestNewsDiscriptionLabel.layer];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(webSiteTapped:)];
    [self setUserInteractionEnabled:YES];
    [self addGestureRecognizer:tapGesture];
    
//    _newsHeadImageView.image = [UIImage imageNamed:[data objectForKey:IMAGE_KEY]];
//    [_websiteButton setImage:[UIImage imageNamed:[data objectForKey:ICON_KEY]] forState:UIControlStateNormal];
//    [_unreadItemsButton setTitle:[data objectForKey:UNREAD_NEWS_COUNT_KEY] forState:UIControlStateNormal];
//    _latestNewsDiscriptionLabel.text = [data objectForKey:DESCRIPTION_KEY];
//    _latestNewsDateLabel.text = [data objectForKey:CREATED_DATE_KEY];
}

- (void)loadData:(NSDictionary *)data
{
    _newsHeadImageView.image = [UIImage imageNamed:[data objectForKey:@"image"]];
    [_websiteButton setImage:[UIImage imageNamed:[data objectForKey:ICON_KEY]] forState:UIControlStateNormal];
    [_unreadItemsButton setTitle:[data objectForKey:UNREAD_NEWS_COUNT_KEY] forState:UIControlStateNormal];
    _latestNewsDiscriptionLabel.text = [data objectForKey:DESCRIPTION_KEY];
    _latestNewsDateLabel.text = [data objectForKey:CREATED_DATE_KEY];
}

- (void)webSiteTapped:(UITapGestureRecognizer *)tapGesture
{
    if ([tapGesture state] == UIGestureRecognizerStateEnded)
    {
        CGPoint point = [tapGesture locationInView:self];
        
        // Tap on avatar button
        CGRect rectButton = _websiteButton.frame;
        
        if (CGRectContainsPoint(rectButton, point))
        {
            [_delegate websiteCollectionViewDidTapWebsite:0];
            return;
        }

//        [_delegate websiteCollectionViewDidTapNew:<#(NSInteger)#> atNewsItem:<#(NSInteger)#>];
        [_delegate websiteCollectionViewDidTapWebsite:0];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    //
    _newsHeadImageView.frame = self.bounds;
    
    int newHeight = self.height/3;
    
    //
    _websiteButton.frame = RECT(10, 10, MIN(60, newHeight), MIN(60, newHeight));
    _websiteButton.layer.cornerRadius = _websiteButton.width/2;
    
    // drop shadow
    [_websiteButton.layer setShadowColor:[UIColor whiteColor].CGColor];
    [_websiteButton.layer setShadowOpacity:0.8];
    [_websiteButton.layer setShadowRadius:_websiteButton.width/2];
    [_websiteButton.layer setShadowOffset:CGSizeMake(-2.0, -2.0)];
//    _websiteButton.layer.frame = CGRectInset(_websiteButton.frame, -8, -8);
    
//    _websiteButton.layer.backgroundColor = [UIColor orangeColor].CGColor;
//    _websiteButton.layer.cornerRadius = 20.0;
//    _websiteButton.layer.frame = CGRectInset(_websiteButton.layer.frame, 20, 20);

    int badgeWH = 24;
    _unreadItemsButton.frame = RECT(_websiteButton.right - badgeWH, _websiteButton.top, badgeWH, badgeWH);
    _unreadItemsButton.layer.cornerRadius = _unreadItemsButton.width/2;
    
    //
    CGRect descriptionRect = RECT(0, self.height - newHeight, self.width, newHeight);;
    _gradientDiscriptionLayer.frame = descriptionRect;
    
    int deflate_w = 10;
    _latestNewsDiscriptionLabel.frame = RECT(descriptionRect.origin.x + deflate_w,
                                             descriptionRect.origin.y,
                                             descriptionRect.size.width - 2*deflate_w,
                                             descriptionRect.size.height - 20);
    
    _latestNewsDateLabel.frame = RECT(_latestNewsDiscriptionLabel.left,
                                             _latestNewsDiscriptionLabel.bottom,
                                             _latestNewsDiscriptionLabel.width,
                                             20);

}

- (void)websiteButtonTapped:(id)sender
{
    [_delegate websiteCollectionViewDidTapWebsite:0];
}
@end
